//
//  ESDrawerViewController.m
//  EasyStyle
//
//  Created by FK on 16/5/8.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESDrawerViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "ESDrawerCell.h"
#import "UIImageView+AFNetworking.h"
#import "ESReadHistoryViewController.h"
#import "ESTabBarViewController.h"
#import "ESCacheClear.h"

static const CGFloat kESDrawerViewControllerCellHeight = 50;

@interface ESDrawerViewController ()<UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *drawerTableView;

@property (nonatomic, strong) NSArray *leftDrawMenuLists;

@property (nonatomic, strong) NSArray *drawerImageArr;

@property (nonatomic, strong) NSArray *calssList;

@property (nonatomic, strong)UILabel *cacheClearTipsView;

@end

@implementation ESDrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHex:@"3ec5dc"];
    
    self.navigationController.navigationBar.hidden = YES;
    
    _leftDrawMenuLists = @[@"阅读历史",@"本地收藏",@"信息反馈",@"清除缓存"];
    
    _drawerImageArr = @[@"history",@"favorites",@"feedback",@"clear"];
    
    _calssList = @[@"ESReadHistoryViewController",@"ESCollectionViewController",@"ESFeedBackViewController",@"ESCacheClear"];
    
    [self setupRootView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -- 布局相关
- (void)setupRootView
{
//    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//    bgImageView.image = [UIImage imageNamed:@"drawerBackground"];
//    [self.view addSubview:bgImageView];
    
    NSInteger startY = StatusBarHeight;
    
    self.drawerTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, startY, ScreenWidth, kESDrawerViewControllerCellHeight * _calssList.count) style:UITableViewStylePlain];
    self.drawerTableView.centerY = self.view.centerY - kESDrawerViewControllerCellHeight * 2;
    self.drawerTableView.backgroundColor = [UIColor clearColor];
    self.drawerTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.drawerTableView setSeparatorColor:[UIColor colorWithHex:@"F5F5F5"]];
    self.drawerTableView.scrollEnabled = NO;
    
    [self.drawerTableView setDelegate:self];
    [self.drawerTableView setDataSource:self];

    [self.view addSubview:self.drawerTableView];
    
    // 缓存清理tips
    _cacheClearTipsView =  [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    _cacheClearTipsView.backgroundColor = [UIColor lightGrayColor];
    _cacheClearTipsView.alpha = 0;
    _cacheClearTipsView.layer.cornerRadius = 4.0f;
    _cacheClearTipsView.clipsToBounds = YES;
    _cacheClearTipsView.center = self.view.center;
    _cacheClearTipsView.textAlignment = NSTextAlignmentCenter;
    _cacheClearTipsView.textColor = [UIColor whiteColor];
    _cacheClearTipsView.text = @"清理完成";
    [self.view addSubview:_cacheClearTipsView];
}

#pragma mark -- TableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _leftDrawMenuLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"drawerCell";
    ESDrawerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [ESDrawerCell loadViewFromSameXibName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.menuName.text = _leftDrawMenuLists[indexPath.row];
    cell.menuImage.image = [UIImage imageNamed:_drawerImageArr[indexPath.row]];
    return cell;
}

#pragma mark -- TabelView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kESDrawerViewControllerCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *className = [self.calssList objectAtIndex:indexPath.row];
    
    if ([className isEqualToString:@"ESCacheClear"]) {

        
        float cache = [ESCacheClear folderSizeAtPath:CacheURL_SandBox];
        NSLog(@"cache === %f",cache);
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString  stringWithFormat:@"当前缓存大小为%.2fM.\n确定要清理缓存吗？",cache] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 1002;
        [alert show];
        

        
    } else {
        [[NSNotificationCenter defaultCenter]postNotificationName:DrawerCellSelectedNOtification object:className];
    }
}

#pragma mark -- UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1002) {
        if (buttonIndex == 1) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [ESCacheClear clearCache:CacheURL_SandBox];

                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [UIView animateWithDuration:1 animations:^{
                        _cacheClearTipsView.alpha = 1;
                        
                    } completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.8 animations:^{
                            _cacheClearTipsView.alpha = 0;
                            
                        } completion:^(BOOL finished) {
                        }];
                    }];
                });
            });
        }
    }
}

@end
